package com.taiyi.renaissancemall.ui.mine.order.pendingpayment;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ActivityUtils;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.adapter.baseadapter.MultiItemTypeAdapter;
import com.taiyi.renaissancemall.base.BaseFragment;
import com.taiyi.renaissancemall.bean.OrderListBean;
import com.taiyi.renaissancemall.ui.mine.order.detail.OrderDetailActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PendingPaymentFragment extends BaseFragment<PendingPaymentView, PendingPaymentPresent> implements PendingPaymentView, SpringView.OnFreshListener, MultiItemTypeAdapter.OnItemClickListener {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    private int pageNo = 1;
    private Dialog mDialog;
    private String currentOrderId;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;

    List<OrderListBean.OrderlistBean> data = new ArrayList<>();

    @Override
    public PendingPaymentPresent initPresenter() {
        return new PendingPaymentPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        springView.setHeader(new DefaultHeader(getContext()));
        springView.setFooter(new DefaultFooter(getContext()));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        mDialog = new Dialog(getContext(), R.style.MyDialog);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.cancel_order_dialog_layout, null);
        mDialog.setContentView(view);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getOrderList();
    }

    @Override
    public void initEvent() {

    }

    @Subscribe
    public void getData(String orderId) {
        if (orderId == "refreshData") {
            springView.callFresh();
        } else if (orderId.startsWith("cancelOrderId")) {
            currentOrderId = orderId.substring(13);
            presenter.cancelOrder();
        }
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.spring_view_layout;
    }

    @Override
    public void getOrderListSuccess(OrderListBean orderListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        List<OrderListBean.OrderlistBean> orderlist = orderListBean.getOrderlist();
        if (pageNo == 1) {
            if (orderlist == null || orderlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
            data.clear();
        }
        data.addAll(orderlist);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        CommonAdapter orderListAdapter = AdapterManager.getOrderListAdapter(getContext(), orderlist, orderListBean.getIfhui());
        recyclerView.setAdapter(orderListAdapter);
        orderListAdapter.setOnItemClickListener(this);
    }

    @Override
    public void getOrderListFailure(String msg) {
        hideProgress();
        toast(msg);
        springView.onFinishFreshAndLoad();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarDarkFont(true, 1.0f).init();
    }

    @Override
    public String getPageNum() {
        return String.valueOf(pageNo);
    }

    @Override
    public String getOrderId() {
        return currentOrderId;
    }

    @Override
    public void cancelOrderSuccess() {
        springView.callFresh();
    }

    @Override
    public void cancelOrderFailure(String msg) {
        toast(msg);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        presenter.getOrderList();
    }

    @Override
    public void onLoadmore() {
        pageNo++;
        presenter.getOrderList();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        String ordercommodityid = data.get(position).getOrdercommodityid();
        String orderid = data.get(position).getOrderid();
        Bundle bundle = new Bundle();
        bundle.putString("ordercommodityid", ordercommodityid);
        bundle.putString("orderType", "0");
        bundle.putString("orderId", orderid);
        ActivityUtils.next(getActivity(), OrderDetailActivity.class, bundle);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
